package org.dromara.authcode.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.authcode.domain.vo.TGoodsVo;
import org.dromara.authcode.domain.bo.TGoodsBo;
import org.dromara.authcode.service.ITGoodsService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 商品
 *
 * @author Miles Meng
 * @date 2025-08-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/authcode/goods")
public class TGoodsController extends BaseController {

    private final ITGoodsService tGoodsService;

    /**
     * 查询商品列表
     */
    @SaCheckPermission("authcode:goods:list")
    @GetMapping("/list")
    public TableDataInfo<TGoodsVo> list(TGoodsBo bo, PageQuery pageQuery) {
        return tGoodsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出商品列表
     */
    @SaCheckPermission("authcode:goods:export")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TGoodsBo bo, HttpServletResponse response) {
        List<TGoodsVo> list = tGoodsService.queryList(bo);
        ExcelUtil.exportExcel(list, "商品", TGoodsVo.class, response);
    }

    /**
     * 获取商品详细信息
     *
     * @param goodsId 主键
     */
    @SaCheckPermission("authcode:goods:query")
    @GetMapping("/{goodsId}")
    public R<TGoodsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long goodsId) {
        return R.ok(tGoodsService.queryById(goodsId));
    }

    /**
     * 新增商品
     */
    @SaCheckPermission("authcode:goods:add")
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TGoodsBo bo) {
        return toAjax(tGoodsService.insertByBo(bo));
    }

    /**
     * 修改商品
     */
    @SaCheckPermission("authcode:goods:edit")
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TGoodsBo bo) {
        return toAjax(tGoodsService.updateByBo(bo));
    }

    /**
     * 删除商品
     *
     * @param goodsIds 主键串
     */
    @SaCheckPermission("authcode:goods:remove")
    @Log(title = "商品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{goodsIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] goodsIds) {
        return toAjax(tGoodsService.deleteWithValidByIds(List.of(goodsIds), true));
    }
}
